﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class QuanLyTheLoai : Form
    {
        public event EventHandler SachEventHandle=null;
        public QuanLyTheLoai()
        {
            InitializeComponent();
        }
        private void SetUpDataGridViewTheLoai()
        {
            dgvTheLoai.BackgroundColor = Color.White;
            dgvTheLoai.ColumnCount = 2;
            dgvTheLoai.Columns[0].Name = "Mã Thể Loại";
            dgvTheLoai.Columns[1].Name = "Tên Thể Loại";

            dgvTheLoai.Columns[0].Width = 80;
            dgvTheLoai.Columns[1].Width = 240;


            dgvTheLoai.Columns[0].ReadOnly = true;
            dgvTheLoai.Columns[1].ReadOnly = true;

            dgvTheLoai.MultiSelect = false;
          //  dgvTheLoai.MultiSelect = true;
            dgvTheLoai.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvTheLoai.DefaultCellStyle.SelectionBackColor = Color.Ivory; //Color.Silver;
            dgvTheLoai.DefaultCellStyle.SelectionForeColor = Color.Black;

        }
        private void LoadDuLieuDataGridViewTheLoai()
        {
            List<TheLoaiDTO> list = new List<TheLoaiDTO>();
            list = TheLoaiService.GetAllTheLoai();
            foreach (TheLoaiDTO theloai in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(dgvTheLoai);



                row.Cells[0].Value = theloai.MaTheLoai.ToString();
                row.Cells[1].Value = theloai.TenTheLoai.ToString();
                dgvTheLoai.Rows.Add(row);

            }
        }

        private void QuanLyTheLoai_Load(object sender, EventArgs e)
        {
            SetUpDataGridViewTheLoai();
            LoadDuLieuDataGridViewTheLoai();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (SachEventHandle != null)
            {
                SachEventHandle(sender, e);
            }
            this.Close();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvTheLoai.SelectedRows[0].IsNewRow==false)
            {
                MessageBox.Show(dgvTheLoai.SelectedRows[0].Index.ToString());
               /* TheLoaiDTO theloaiDTO = new TheLoaiDTO();
                theloaiDTO.MaTheLoai = int.Parse(dgvTheLoai.SelectedRows[0].Cells[0].Value.ToString());
                theloaiDTO.TenTheLoai = dgvTheLoai.SelectedRows[0].Cells[1].Value.ToString();*/
                int matheloai = int.Parse(dgvTheLoai.SelectedRows[0].Cells[0].Value.ToString());
                DialogResult r=MessageBox.Show("Bạn thực sự muốn xóa?","Thông báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
                if(r==DialogResult.Yes)
                {
                    if (TheLoaiService.Delete(matheloai))
                    {
                        dgvTheLoai.Rows.RemoveAt(dgvTheLoai.SelectedRows[0].Index);
                        MessageBox.Show("Đã xóa thành công", "Thông báo");
                    }
                    else
                    {
                        MessageBox.Show("Không được xóa thể loại này", "Thông báo");
                    }
                }

            }
        }

        private void btnCapNhatTheLoai_Click(object sender, EventArgs e)
        {
          //  this.IsMdiContainer = true;
            CapNhatTheLoai frm = new CapNhatTheLoai();
          frm.CapNhatTheLoaiEventHandle+=new EventHandler(Refesh);
          
            frm.Show();
           
           
            
        }
        void Refesh(object sender, EventArgs e)
        {
            dgvTheLoai.Rows.Clear();
            LoadDuLieuDataGridViewTheLoai();
        }

        private void btnThemTheLoai_Click(object sender, EventArgs e)
        {
            ThemTheLoai frm = new ThemTheLoai();

            frm.Show();

        }

        private void QuanLyTheLoai_AutoValidateChanged(object sender, EventArgs e)
        {
            MessageBox.Show("autovalidatechanged");
        }

        
    }
}
